package com.zsh.wanandroid.utils;

import android.util.Log;

/**
 * @author zsh
 * @date 2019/10/21.
 * 功能：Log日志
 */
public class LogUtils {

    //TAG
    private static String TAG = "LogUtils_";
    // 开关
    private static boolean debug = true;

    private LogUtils() {}

    /**
     * 设置Log的tag，默认是LogUtils_
     * @param TAG
     */
    public static void setTAG(String TAG) {
        LogUtils.TAG = TAG;
    }

    /**
     * 是否 debug 版本
     * @param debug  true 是调试版本；false 是正式版本
     */
    public static void setDebug(boolean debug) {
        LogUtils.debug = debug;
    }

    public static void v(String msg) {
        v(msg, null);
    }

    public static void v(String msg, Throwable t) {
        StackTraceElement[] trace = Thread.currentThread().getStackTrace();
        int methodCount = 1;
        int stackOffset = getStackOffset(trace);
        if (methodCount + stackOffset > trace.length) {
            methodCount = trace.length - stackOffset - 1;
        }
        if (debug) {
            for (int i = methodCount; i > 0; i--) {
                int stackIndex = i + stackOffset;
                if (stackIndex >= trace.length) {
                    continue;
                }
                StackTraceElement element = trace[stackIndex];

                StringBuilder builder = new StringBuilder();
                builder.append(getSimpleClassName(element.getClassName()))
                        .append(".")
                        .append(element.getMethodName())
                        .append(" ")
                        .append(" (")
                        .append(element.getFileName())
                        .append(":")
                        .append(element.getLineNumber())
                        .append(")")
                        .append(" | ")
                        .append(msg);
                Log.v(TAG+getSimpleClassName(element.getClassName()), builder.toString());
            }
        }
    }

    public static void d(String msg) {
        d(msg, null);
    }

    public static void d(String msg, Throwable t) {
        StackTraceElement[] trace = Thread.currentThread().getStackTrace();
        int methodCount = 1;
        int stackOffset = getStackOffset(trace);
        if (methodCount + stackOffset > trace.length) {
            methodCount = trace.length - stackOffset - 1;
        }
        if (debug) {
            for (int i = methodCount; i > 0; i--) {
                int stackIndex = i + stackOffset;
                if (stackIndex >= trace.length) {
                    continue;
                }
                StackTraceElement element = trace[stackIndex];

                StringBuilder builder = new StringBuilder();
                builder.append(getSimpleClassName(element.getClassName()))
                        .append(".")
                        .append(element.getMethodName())
                        .append(" ")
                        .append(" (")
                        .append(element.getFileName())
                        .append(":")
                        .append(element.getLineNumber())
                        .append(")")
                        .append(" | ")
                        .append(msg);
                Log.d(TAG+getSimpleClassName(element.getClassName()), builder.toString());
            }
        }
    }

    public static void i(String msg) {
        i(msg, null);
    }

    public static void i(String msg, Throwable t) {
        StackTraceElement[] trace = Thread.currentThread().getStackTrace();
        int methodCount = 1;
        int stackOffset = getStackOffset(trace);
        if (methodCount + stackOffset > trace.length) {
            methodCount = trace.length - stackOffset - 1;
        }
        if (debug) {
            for (int i = methodCount; i > 0; i--) {
                int stackIndex = i + stackOffset;
                if (stackIndex >= trace.length) {
                    continue;
                }
                StackTraceElement element = trace[stackIndex];

                StringBuilder builder = new StringBuilder();
                builder.append(getSimpleClassName(element.getClassName()))
                        .append(".")
                        .append(element.getMethodName())
                        .append(" ")
                        .append(" (")
                        .append(element.getFileName())
                        .append(":")
                        .append(element.getLineNumber())
                        .append(")")
                        .append(" | ")
                        .append(msg);
                Log.i(TAG+getSimpleClassName(element.getClassName()), builder.toString());
            }
        }
    }

    public static void w(String msg) {
        w(msg, null);
    }

    public static void w(String msg, Throwable t) {
        StackTraceElement[] trace = Thread.currentThread().getStackTrace();
        int methodCount = 1;
        int stackOffset = getStackOffset(trace);
        if (methodCount + stackOffset > trace.length) {
            methodCount = trace.length - stackOffset - 1;
        }
        if (debug) {
            for (int i = methodCount; i > 0; i--) {
                int stackIndex = i + stackOffset;
                if (stackIndex >= trace.length) {
                    continue;
                }
                StackTraceElement element = trace[stackIndex];

                StringBuilder builder = new StringBuilder();
                builder.append(getSimpleClassName(element.getClassName()))
                        .append(".")
                        .append(element.getMethodName())
                        .append(" ")
                        .append(" (")
                        .append(element.getFileName())
                        .append(":")
                        .append(element.getLineNumber())
                        .append(")")
                        .append(" | ")
                        .append(msg);
                Log.w(TAG+getSimpleClassName(element.getClassName()), builder.toString());
            }
        }
    }

    public static void e(String msg) {
        e(msg, null);
    }

    public static void e(String msg, Throwable t) {
        StackTraceElement[] trace = Thread.currentThread().getStackTrace();
        int methodCount = 1;
        int stackOffset = getStackOffset(trace);
        if (methodCount + stackOffset > trace.length) {
            methodCount = trace.length - stackOffset - 1;
        }
        if (debug) {
            for (int i = methodCount; i > 0; i--) {
                int stackIndex = i + stackOffset;
                if (stackIndex >= trace.length) {
                    continue;
                }
                StackTraceElement element = trace[stackIndex];

                StringBuilder builder = new StringBuilder();
                builder.append(getSimpleClassName(element.getClassName()))
                        .append(".")
                        .append(element.getMethodName())
                        .append(" ")
                        .append(" (")
                        .append(element.getFileName())
                        .append(":")
                        .append(element.getLineNumber())
                        .append(")")
                        .append(" | ")
                        .append(msg);
                Log.e(TAG+getSimpleClassName(element.getClassName()), builder.toString());
            }
        }
    }

    private static String getSimpleClassName(String name) {
        int lastIndex = name.lastIndexOf(".");
        return name.substring(lastIndex + 1);
    }

    private static int getStackOffset(StackTraceElement[] trace) {
        for (int i = 2; i < trace.length; i++) {
            StackTraceElement e = trace[i];
            String name = e.getClassName();
            if (!name.equals(LogUtils.class.getName())) {
                return --i;
            }
        }
        return -1;
    }

}
